//
//  String+Extension.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/21.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

extension String{

    var systemEmoticon : String {
        
        //使用扫描器.扫描指定的字符串
        
        let scanner = NSScanner(string: self)
        
        //扫描字符串中的16进制字符串
        
        //定义存储指针
        var valuePoint : UInt32 = 0
        
        scanner.scanHexInt(&valuePoint)
        
        //将16进制字符串转换成Unicode字符 串
        let result = Character(UnicodeScalar(valuePoint))
        
        return "\(result)"
        
    }
    
    //使用正则表达式获得来源链接
    func getSourceStringWithRegularExpression() -> (htmlLinkStr : String? , sourceStr : String?){
        
        //建立解决方案
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        
        //设置正则表达式
        let regular = try! NSRegularExpression(pattern: pattern, options: [])
        
        //进行正则运算
        let result = regular.firstMatchInString(self, options: [], range: NSRange(location: 0, length: characters.count))
        
        print(result)
        
        print(result?.numberOfRanges)
        
        print("\(result?.rangeAtIndex(0)) || \(result?.rangeAtIndex(1)) || \(result?.rangeAtIndex(2))")
        
        var resultStringLink : String?
        
        var resultStringSource : String?
        
        if let rangeIndex1 = result?.rangeAtIndex(1) , rangeIndex2 = result?.rangeAtIndex(2){
            
            resultStringLink = (self as NSString).substringWithRange(rangeIndex1)
            
            resultStringSource = (self as NSString).substringWithRange(rangeIndex2)
            
        }
        
        return(resultStringLink , resultStringSource)
        
    }

    
    

}
